/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package client;

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;

/**
 *
 * @author Vlada, Aleksandar
 */
public class ClientForm extends JFrame {

    static int port = 4040;
    
    JLabel pictureLabel;
    int singleClick = 1;
    int doubleClick = 2;
    int dragAndDrop = 3;
    int keyboard = 4;
    int startXforDragAndDrop = 0;
    int startYforDragAndDrop = 0;
    int wFinal;
    int hFinal;
    double percent;
    ObjectOutputStream oos;

    public ClientForm(int wFinal, int hFinal, String host, double percent) {

        try {
            this.setLayout(null);
            this.setSize(wFinal, hFinal+30);
            pictureLabel = new JLabel();
            this.add(pictureLabel);
            pictureLabel.setBounds(0, 0, wFinal, hFinal);
            this.percent = percent;
            
            Socket socket = new Socket(host, port);
            oos = new ObjectOutputStream(socket.getOutputStream());

            pictureLabel.addMouseListener(new MouseListener() {
                public void mouseClicked(MouseEvent e) {
                    if (e.getClickCount() == 1) {
                        try {
                            int x = e.getX();
                            int y = e.getY();
                            int button = e.getButton();
                            oos.writeInt(singleClick);
                            oos.flush();
                            oos.writeInt(calculatePosition(x));
                            oos.flush();
                            oos.writeInt(calculatePosition(y));
                            oos.flush();
                            oos.writeInt(button);
                            oos.flush();
                        } catch (UnknownHostException ex) {
                            Logger.getLogger(ClientForm.class.getName()).log(Level.SEVERE, null, ex);
                        } catch (IOException ex) {
                            Logger.getLogger(ClientForm.class.getName()).log(Level.SEVERE, null, ex);
                        }
                    } else if (e.getClickCount() == 2) {
                        try {
                            int x = e.getX();
                            int y = e.getY();
                            int button = e.getButton();
                            oos.writeInt(doubleClick);
                            oos.flush();
                            oos.writeInt(calculatePosition(x));
                            oos.flush();
                            oos.writeInt(calculatePosition(y));
                            oos.flush();
                            oos.writeInt(button);
                            oos.flush();
                        } catch (UnknownHostException ex) {
                            Logger.getLogger(ClientForm.class.getName()).log(Level.SEVERE, null, ex);
                        } catch (IOException ex) {
                            Logger.getLogger(ClientForm.class.getName()).log(Level.SEVERE, null, ex);
                        }
                    }
                }

                public void mousePressed(MouseEvent e) {
                    if (e.getButton() == 1) {
                        startXforDragAndDrop = e.getX();
                        startYforDragAndDrop = e.getY();
                    }
                }

                public void mouseReleased(MouseEvent e) {
                    if (e.getButton() == 1) {
                        try {
                            int endX = e.getX();
                            int endY = e.getY();
                            oos.writeInt(dragAndDrop);
                            oos.flush();
                            oos.writeInt(calculatePosition(startXforDragAndDrop));
                            oos.flush();
                            oos.writeInt(calculatePosition(startYforDragAndDrop));
                            oos.flush();
                            oos.writeInt(calculatePosition(endX));
                            oos.flush();
                            oos.writeInt(calculatePosition(endY));
                            oos.flush();
                        } catch (UnknownHostException ex) {
                            Logger.getLogger(ClientForm.class.getName()).log(Level.SEVERE, null, ex);
                        } catch (IOException ex) {
                            Logger.getLogger(ClientForm.class.getName()).log(Level.SEVERE, null, ex);
                        }
                    }
                }

                public void mouseEntered(MouseEvent e) {
                }

                public void mouseExited(MouseEvent e) {
                }
            });

            this.addKeyListener(new KeyListener() {
                public void keyTyped(KeyEvent e) {
                }

                public void keyPressed(KeyEvent e) {
                    try {
                        oos.writeInt(keyboard);
                        oos.flush();
                        oos.writeInt(e.getKeyCode());
                        oos.flush();
                        oos.writeBoolean(e.isShiftDown());
                        oos.flush();
                        oos.writeBoolean(e.isControlDown());
                        oos.flush();
                    } catch (IOException ex) {
                        Logger.getLogger(ClientForm.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }

                public void keyReleased(KeyEvent e) {
                }
            });
        } catch (IOException e1) {
            System.err.println(e1.getMessage());
        }
    }

    void setIcon(ImageIcon icon) {
        pictureLabel.setIcon(icon);
    }

    int calculatePosition(int i) {
        return (int) (i * 100.0 / percent);
    }
}